package com.kyub.spring.gears.web;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.context.request.WebRequest;
import org.springframework.ui.ModelMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.kyub.spring.gears.model.PojoDescriptor;
import com.kyub.spring.gears.model.PojoRegistry;
import com.kyub.spring.gears.model.CollectionPropertyDescriptor;
import com.kyub.spring.gears.web.upload.FileUploadBean;
import com.kyub.spring.gears.PrjLogger;
import com.kyub.spring.gears.util.IntrospectionUtils;

import java.io.Serializable;

@Controller("RiaCrudController")
@SessionAttributes({"commandPojo", "commandUpload"})
public class RiaCrudController extends ControllerAncestor{

     @Autowired
    private PojoRegistry pojoRegistry;
    @Autowired
    private CrudControllerConfig crudConfig;

    @RequestMapping("/{pojo}/rialist")
    public ModelAndView list(ModelMap model, @PathVariable("pojo") String pojo, WebRequest request) {
        ModelAndView v =  prepareListModel(pojo, request, "/listPure", true, "popup");

        return v;
    }
     @RequestMapping("/{pojo}/riaview/{id}")
    public ModelAndView viewRia(@PathVariable(PARAM_ID_NAME) Serializable id, ModelMap model, @PathVariable("pojo") String pojo) {
        ModelAndView v = buildModelAndView();

        v.addObject("pojoDescriptor", loadPojoDescriptor(pojo));

        v.addObject("pojo", pojoRegistry.getManager(pojo).load(id));
        v.setViewName("/" + crudConfig.templateFolder + "/viewPure");
        return v;
    }

    @RequestMapping(value = "/{pojo}/riaupdate/{id}", method = RequestMethod.GET)
    public ModelAndView setupPopulatedForm(ModelMap model, WebRequest request, @PathVariable(PARAM_ID_NAME) Serializable id, @PathVariable("pojo") String pojo) {


        ModelAndView v = buildModelAndView();
         v.setViewName("/" + crudConfig.templateFolder + "/formPure");
        v.addObject("commandUpload", new FileUploadBean());
        v.addObject("updateObject", true);


        final PojoDescriptor descriptor = loadPojoDescriptor(pojo);
        v.addObject(COMMAND_POJO_NAME, pojoRegistry.getManager(pojo).load(id));
        v.addObject("relatedValues", this.getFactory().loadRealtedValues(descriptor, pojoRegistry));
        v.addObject("pojoDescriptor", descriptor);

        return v;
    }

    @RequestMapping("/{pojo}/riaviewrelated/{id}/{property}")
    public ModelAndView viewRelated(@PathVariable(PARAM_ID_NAME) long id, ModelMap model, @PathVariable("pojo") String pojo, @PathVariable("property") String property) {
        ModelAndView v = buildModelAndView();

        final PojoDescriptor pojoOwner = loadPojoDescriptor(pojo);
        v.addObject("pojoOwner", pojoOwner);
         PrjLogger.debug(CrudController.class,"pojoOwner " + pojoOwner);


        final CollectionPropertyDescriptor propertyDescriptor = (CollectionPropertyDescriptor) pojoOwner.getProperty(property);
         PrjLogger.debug(CrudController.class,"pojoDescriptor for property " + property + " " + propertyDescriptor.getPojoDescr());
        v.addObject("pojoDescriptor", propertyDescriptor.getPojoDescr());


        try {
            v.addObject("pojoList", IntrospectionUtils.readPropertyValue(pojoRegistry.getManager(pojo).load(id), property));
        } catch (Exception e) {
             PrjLogger.debug(CrudController.class,e.toString());
            // if (PrjLogger.isDebugEnabled())
            e.printStackTrace();
        }
        v.setViewName("/" + crudConfig.templateFolder + "/viewrelated");
        return v;
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
      super.initBinderDelegate(binder);

    }

    public PojoRegistry getPojoRegistry() {
        return pojoRegistry;
    }

    public void setPojoRegistry(PojoRegistry pojoRegistry) {
        this.pojoRegistry = pojoRegistry;
    }

    public CrudControllerConfig getCrudConfig() {
        return crudConfig;
    }

    public void setCrudConfig(CrudControllerConfig crudConfig) {
        this.crudConfig = crudConfig;
    }
}
